package com.scau.hwadee.examination.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.hwadee.examination.entity.Course;
import com.scau.hwadee.examination.entity.Coursecorrespondingrelation;
import com.scau.hwadee.examination.entity.Nationwidecourse;
import com.scau.hwadee.examination.service.CourseManagerService;
import com.scau.hwadee.examination.service.TextbookService;
import com.scau.hwadee.examination.utils.ExportExcelUtils;
import com.scau.hwadee.examination.utils.ImportExcelUtils;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.utils.UniqueIDGeneratorUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author YHX
 * @date 2019/7/30 20:07
 * description
 */
@Slf4j
@Controller
@RequestMapping("course/management")
@Api(tags = "CourseManagementController", description = "课程管理模块")
public class CourseManagementController {

    @Autowired
    private CourseManagerService courseManagerService;

    @Autowired
    private TextbookService textbookService;

    @PostMapping("importCourseExcel")
    @ApiOperation("导入课程Excel表")
    public ResponseEntity<ResponseVo> importExcel(MultipartFile file) {
        log.debug("导入课程Excel表");
        ResponseVo vo = ResponseVo.instance();
        try {
            List<Course> courses = ImportExcelUtils.parseCourseExcel(file.getInputStream());
            int cnt = courseManagerService.insertCourseListByExcel(courses);
            String message;
            if (cnt > 0) {
                message = "导入Excel表成功";
            } else {
                message = "导入Excel表失败";
            }
            vo.setMessage(message);
        } catch (Exception e) {
            vo.setMessage("导入Excel表失败");
        }
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("importNationalCourseExcel")
    @ApiOperation("导入全国课程Excel表")
    public ResponseEntity<ResponseVo> importNationalCourseExcel(MultipartFile file) {
        log.debug("导入全国课程Excel表");
        ResponseVo vo = ResponseVo.instance();
        try {
            List<Nationwidecourse> courses = ImportExcelUtils.parseNationalCourseExcel(file.getInputStream());
            int cnt = courseManagerService.insertNationalCourseListByExcel(courses);
            String message;
            if (cnt > 0) {
                message = "导入Excel表成功";
            } else {
                message = "导入Excel表失败";
            }
            vo.setMessage(message);
        } catch (Exception e) {
            vo.setMessage("导入Excel表失败");
        }
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("exportNationalCourseExcel")
    @ApiOperation(value = "导出全国课程Excel表")
    public ResponseEntity<byte[]> exportNationalCourseExcel() {
        log.debug("导出全国课程Excel表");
        return ExportExcelUtils.exportNationalCourse2Excel(courseManagerService.getAllNationalCourse(1, 99999999).getList());
    }

    @GetMapping(value = "exportExcel", produces = "application/json;charset=UTF-8")
    @ApiOperation("导出课程Excel表")
    public ResponseEntity<byte[]> exportExcel() {
        log.debug("导出课程Excel表");
        return ExportExcelUtils.exportSCProvinceCourse2Excel(courseManagerService.getAllCourses2ExportExcel());
    }

    @PostMapping("newCourse")
    @ApiOperation("增加新的课程")
    public ResponseEntity<ResponseVo> createNewCourse(@RequestBody String json) {
        log.debug("增加新的课程");
        log.debug("json is " + json);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Course course = JsonUtils.JSONToObject(json, Course.class, format);
        ResponseVo vo = ResponseVo.instance();
        int cnt = courseManagerService.insertNewCourse(course);
        if (cnt > 0) {
            vo.setMessage("增加课程成功");
        } else {
            vo.setMessage("增加课程失败");
        }
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("/courseState")
    @ApiOperation("课程启用和停用")
    public ResponseEntity<ResponseVo> courseState(@RequestBody String param) {
        log.debug("课程的启用停用");
        ResponseVo vo = ResponseVo.instance();
        JSONArray courses = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray");
        int cnt = courseManagerService.updateCourseState(courses);
        String message;
        message = "成功修改课程数为：" + cnt;
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allCourses")
    @ApiOperation(value = "获取全部的课程信息")
    public ResponseEntity<ResponseVo> getAllCourseMessage(@RequestParam("pageNum") int pageNum,
                                                          @RequestParam("pageSize") int pageSize) {
        log.debug("获取全部的课程信息");
        ResponseVo vo = ResponseVo.instance();
        vo.setData(courseManagerService.getAllCourses(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("textbookOfCourse")
    @ApiOperation(value = "获取某课程的教材信息")
    public ResponseEntity<ResponseVo> getAllTextbookOfCourse(@RequestParam("courseId") String courseId) {
        log.debug("获取某课程的教材信息");
        ResponseVo vo = ResponseVo.instance();
        vo.setData(courseManagerService.getTextbookOfCourse(courseId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("/designatedTextbook")
    @ApiOperation(value = "修改课程的指定教材")
    public ResponseEntity<ResponseVo> designatedTextbookforCourse(@RequestBody Map<String, String> params) {
        log.debug("修改课程的指定教材");
        ResponseVo vo = ResponseVo.instance();
        String courseId = params.get("courseId");
        String textbookId = params.get("textbookId");
        int cnt = courseManagerService.updateCourseTextbook(courseId, textbookId);
        String message;
        if (cnt > 0) {
            message = "为课程指定教材成功";
        } else {
            message = "为课程指定教材失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("newNationalCourse")
    @ApiOperation(value = "新增全国课程")
    public ResponseEntity<ResponseVo> createNewNationalCourse(@RequestBody Nationwidecourse nationwidecourse) {
        log.debug("新增全国课程");
        ResponseVo vo = ResponseVo.instance();
        int cnt = courseManagerService.createNewNationalCourse(nationwidecourse);
        String message;
        if (cnt > 0) {
            message = "新增全国课程成功";
        } else {
            message = "新增全国课程失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allNationalCourses")
    @ApiOperation(value = "获取全部全国课程")
    public ResponseEntity<ResponseVo> getAllNationalCourses(@RequestParam("pageNum") int pageNum,
                                                            @RequestParam("pageSize") int pageSize) {
        ResponseVo vo = ResponseVo.instance();
        log.debug("获取全部全国课程");
        vo.setData(courseManagerService.getAllNationalCourse(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allPracticalOrTheoreticalCourse")
    @ApiOperation("获取全部的理论或者实践课程")
    public ResponseEntity<ResponseVo> getAllPracticalOrTheoreticalCourse(@RequestParam("pageNum") int pageNum,
                                                                         @RequestParam("pageSize") int pageSize,
                                                                         @RequestParam("property") String property,
                                                                         @RequestParam("mvId") String mvId) {
        ResponseVo vo = ResponseVo.instance();
        log.debug("获取全部的理论或者实践课程");
        vo.setData(courseManagerService.getAllPracticalOrTheoreticalCourse(pageNum, pageSize, property, mvId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allTextbooks")
    @ApiOperation("获取全部的教材")
    public ResponseEntity<ResponseVo> getAllTextbooks(@RequestParam("pageNum") int pageNum,
                                                      @RequestParam("pageSize") int pageSize) {
        ResponseVo vo = ResponseVo.instance();
        log.debug("获取全部的教材");
        vo.setData(textbookService.getAllTextbook(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @DeleteMapping("nationalCourse")
    @ApiOperation(value = "删除全国课程")
    public ResponseEntity<ResponseVo> deleteNationalCourse(@RequestBody String data) {
        log.debug("删除全国课程");
        ResponseVo vo = ResponseVo.instance();
        JSONArray param = Objects.requireNonNull(JsonUtils.JSONToObject(data, JSONObject.class)).getJSONArray("jsonArray");
        int cnt = courseManagerService.deleteNationCourseList(param);
        String message;
        if (cnt > 0) {
            message = "删除全国课程成功";
        } else {
            message = "删除全国课程失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping("newNationalCourse")
    @ApiOperation(value = "修改全国课程")
    public ResponseEntity<ResponseVo> updateNationalCourse(@RequestBody Nationwidecourse nationwidecourse) {
        log.debug("修改全国课程");
        ResponseVo vo = ResponseVo.instance();
        int cnt = courseManagerService.updateNationalCourse(nationwidecourse);
        String message;
        if (cnt > 0) {
            message = "修改全国课程成功";
        } else {
            message = "修改全国课程失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("CourseCorrespondingRelation")
    @ApiOperation("设置理论实践对应关系")
    public ResponseEntity<ResponseVo> setCourseCorrespondingRelation(@RequestBody JSONArray jsonArray) {
        UniqueIDGeneratorUtils.UUIDUniqueIDGeneratorUtils generatorUtils = new UniqueIDGeneratorUtils.UUIDUniqueIDGeneratorUtils();
        List<Coursecorrespondingrelation> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject data = jsonArray.getJSONObject(i);
            Coursecorrespondingrelation c = new Coursecorrespondingrelation();
            c.setCcrId(generatorUtils.generatorId());
            c.setMvId(data.get("versionId").toString());
            c.setPracticalcourseId(data.get("practicalcourseId").toString());
            c.setTheorycourseId(data.get("theorycourseId").toString());
            list.add(c);
        }
        int cnt = courseManagerService.insertCourseCorrespondingRelationList(list);
        String message;
        if (cnt > 0) {
            message = "设置课程成功";
        } else {
            message = "设置课程失败";
        }
        ResponseVo vo = ResponseVo.instance();
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

}
